<?php
include '../config/database.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SFPlatform - Process All Withdarawl Requests</title>
<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif; 
font-size:13px;
}
.info, .success, .warning, .error, .validation {
border: 1px solid;
margin: 15% auto;
padding:15px;
font-weight:bold;
font-size:14px;
background-repeat: no-repeat;
width:400px;
text-align:center;
}
.info {
color: #00529B;
background-color: #BDE5F8;
}
.success {
color: #4F8A10;
background-color: #DFF2BF;
}
.warning {
color: #9F6000;
background-color: #FEEFB3;
}
.error {
color: #D8000C;
background-color: #FFBABA;
}
.alert-box {	
	/* css3 drop shadow */
	-webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
	-moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
	
	/* css3 border radius */
	-moz-border-radius: 5px;
    -webkit-border-radius: 5px;
	
	background:#eee;
	/* styling of the dialog box, i have a fixed dimension for this demo */ 
	width:500px;
	margin:15% auto;
	text-align:cetner;
}

.alert-box .alert-content {
	/* style the content */
	padding:10px; 
	margin:13px;
	color:#666; 
	font-family:arial;
	font-size:14px; 
}
.submit{
	margin: 0 150px;
}
.submit input {
	/* styles for button */
	margin:10px;
	text-align:center;
	width:50px;
	padding: 5px 10px 6px;
	color: #fff;
	text-decoration: none;
	font-weight: bold;
	line-height: 1;
	border:0px;
	float:left;
	
	/* button color */
	background-color: #e33100;
	
	/* css3 implementation :) */
	/* rounded corner */
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	
	/* drop shadow */
	-moz-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.5);
	
	/* text shaow */
	text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
	border-bottom: 1px solid rgba(0,0,0,0.25);
	cursor: pointer;
	
}
.submit input.success {
	background-color: #098312;	
}
.submit input:hover {
	background-color: #c33100;	
}
.submit input.success:hover {
	background-color: #07af13;	
}
/* extra styling */
.alert-box h2 {
	font-weight:700;
	font-size:15px;
    margin:0;
	padding:10px;
}

.alert-box p {
	margin:0;
	padding:10px;
}

.alert-box ul {
	margin:10px 0 10px 20px; 
	padding:0; 
	height:50px;
}
/** Clearfix Hacks */
.clearfix:after {
	clear: both;
	content: '.';
	display: block;
	visibility: hidden;
	height: 0;
}
.clearfix {
	display: inline-block;
	display: block;
}
* html .clearfix {
	height: 1%;
}
*+html .clearfix {
	display: inline-block;
}
</style>
</head>
<body>
<?php
if (empty($_POST)) {
?>
<form method="post" class="alert-box" action="withdraw.php">
<div class="confirm alert-content">
    <h2> Are you sure want to force withdraw requests for all users?</h2>
    <p> This will mimic withdraw request for all users who have balance in their wallet. You'll then have to process these withdrawal requests from Admin Control Panel.</p>
	
    <div class="submit clearfix">
        <input type="submit" value="Yes" name="withdraw-yes" class="success">
        <input type="submit" value="No" name="withdraw-no">
    </div>
</div>
</form>
<?php } ?>
<?php
if (!empty($_POST['withdraw-yes'])) {
$database = 'default';
if (empty($_SERVER['SERVER_ADDR']) || $_SERVER['SERVER_ADDR'] != '127.0.0.1') {
    $database = 'master';
}
$db_config_obj = new DATABASE_CONFIG;
$db_info = array();
$db_data = $db_config_obj->$database;
$hostname = $db_data['host'];
$username = $db_data['login'];
$password = $db_data['password'];
$database = $db_data['database'];
$link = @mysql_connect($hostname, $username, $password);
$is_processed  = true;
if ($link) {
   $db_selected = @mysql_select_db($database, $link);
   if ($db_selected) {
       $result_user_account_amounts = mysql_query("select * from users where available_wallet_amount > 0 ", $link);
       $is_ok = mysql_num_rows($result_user_account_amounts);
       if ($result_user_account_amounts and $is_ok) {
        while ($row = mysql_fetch_assoc($result_user_account_amounts)) {
            // Insert into user cash withdarawels
            $insert_user_cash_withdrawels = mysql_query("INSERT INTO user_cash_withdrawals (created, modified, user_id, amount, withdrawal_status_id) VALUES ('".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s')."', '".$row['id']."', '".$row['available_wallet_amount']."',1)", $link);
            if (!$insert_user_cash_withdrawels) {
                $is_processed = false;
                $mysql_error =  mysql_error();
            }
            // Update in to users table
            $update_users_balance = mysql_query("UPDATE users set available_wallet_amount = 0 WHERE id = ".$row['id'], $link);
        }
       }
       else if (!$is_ok) {
            $is_processed = false;
            $mysql_error =  'No users found having account balance';
       }
       else {
        $is_processed = false;
        $mysql_error =  mysql_error();
       }
   }
   else {
     $is_processed = false;
     $mysql_error =  mysql_error();
   }
}
else {
    $is_processed = false;
    $mysql_error =  mysql_error();
}
}
?>
<?php
if (!empty($_POST['withdraw-yes']) and $is_processed) {
?>
<div class="success">
<p> All withdrawals processed successfully.</p>
</div>
<?php
}
else if (!empty($_POST['withdraw-no'])) {
?>
<div class="warning">
<p> Ok. Your withdrawals are not yet processed. </p>
</div>
<?php
}
else if (!empty($_POST) and !$is_processed) {
?>
<div class="error">
<p> Problem in process withdrawal requests . Getting the following error</p>
<p><?php echo $mysql_error; ?></p>
</div>
<?php
}
?>
</body>
</html>
